<?php


namespace app\controller;


use app\BaseController;
use app\library\Auth;
use think\facade\Log;

class Datacenter extends BaseController{

    /**
     * 权限控制类
     * @var Auth
     */
    protected $auth = null;
    var $data;

    public function _initialize(){
        parent::_initialize();
        $actionname = strtolower($this->request->action());
        $this->auth=new Auth();
        if($actionname!='login'){
            $token = $this->request->server('Authorization', '');
            if($token){
                $token=explode(" ",$token);
                $this->auth->init($token[1]);
                if (!$this->auth->isLogin()) {
                    $this->error_login();
                }
            }else{
                $this->error_login();
            }
        }
        $recive= file_get_contents('php://input');
        Log::info($actionname."：：：".$recive);
        $this->data=json_decode($recive);
    }

    public function login($email,$password){
        $ret=$this->auth->login($email,$password,false);
        if ($ret) {
            $this->auth->keeptime(259200);
            $info=$this->auth->getUserinfo();
            $data = ['token' => $info['token'],'expires'=>259200];
            $this->success(lang('Logged in successful'), $data);
        } else {
            $this->res('-2',$this->auth->getError());
        }
    }

    public function userin(){
        $_face=new \app\model\MqttUserManage();
        foreach ($this->data as $single){
            //按照小区来存储照片
            $path=config('capture.capture_path').'/picbyaizone/'.$single['ai_code'].'/'.date('Ym').'/'.date('d').'/';
            $data['address_id']=$single['ai_code'];
            $data['ywlsh']=$single['uuid'];
            $data['address_wherefrom']=$single['where_from'];
            $data['name']=$single['name'];
            $data['phone']=$single['phone'];
            $data['address']=$single['address'];
            $data['idCard']=$single['id_card'];
            $data['source']=$single['data_from'];
            $data['type']=$single['user_type'];
            $data['photo']=$path;
            $_face->save($data);
            save_base_img($_face->id.'_bg',$single['pic_res'],$path);
        }
    }

    public function facelogin(){
        $_face=new \app\model\MqttFacePushInfo();
        foreach ($this->data as $single){
            //按照小区来存储照片
            $path=config('capture.capture_path').'/'.$single['device_sn'].'/'.date('Ym').'/'.date('d').'/';
            $data['face_id']=$single['uuid'];
            $data['device_id']=$single['device_sn'];
            $data['img_patch']=$path;
            $data['create_time']=$single['timestamp'];
            $_face->save($data);
            save_base_img($_face->id.'_face',$single['face_res'],$path);
            save_base_img($_face->id.'_bg',$single['snap_res'],$path);
        }
    }

    public function zonein(){

    }

    public function capturein(){
        $_face=new \app\model\Faces();
        foreach ($this->data as $single){
            //按照小区来存储照片
            $path=config('capture.capture_path').'/'.$single['device_sn'].'/'.date('Ym').'/'.date('d').'/';
            $data['device_id']=$single['device_sn'];
            $data['file_path']=$path;
            $data['create_time']=$single['timestamp'];
            $_face->save($data);
            save_base_img($_face->id.'_face',$single['face_res'],$path);
            save_base_img($_face->id.'_bg',$single['snap_res'],$path);
        }
    }

    public function devicein(){
        $_device=new \app\model\ViidDevice();
        $data['device_id']=$this->data['device_sn'];
        $data['name']=$this->data['device_name'];
        $aizone=\app\model\Aizone::where('name_code',$this->data['ai_code'])->find();
        if(!$aizone){
            $aizone=\app\model\Aizone::create(['where_from'=>$this->data['device_wherefrom'],'name_code'=>$this->data['ai_code']]);
        }
        $data['aizone_id']=$aizone->id;
        $data['address']=$this->data['device_address'];
        $data['longitude']=$this->data['device_gps_log'];
        $data['latitude']=$this->data['device_gps_lat'];
        $data['device_type']=$this->data['device_type'];
        $data['device_gps_lat']=$this->data['device_sn'];
        $data['device_gps_lat']=$this->data['device_sn_1400'];

        $data['created_at']=date('Y-m-d H:i:s');
        $_device->save($data);
    }

    public function vehlogin(){
        $_face=new \app\model\MqttCarPushInfo();
        foreach ($this->data as $single){
            //按照小区来存储照片
            $path=config('capture.capture_path').'/'.$single['device_sn'].'/'.date('Ym').'/'.date('d').'/';
            $data['device_id']=$single['device_sn'];
            $data['license']=$single['license'];
            $data['photo_url']=$path;
            $data['create_time']=$single['timestamp'];
            $_face->save($data);
            save_base_img($_face->id.'_small',$single['small_res'],$path);
            save_base_img($_face->id.'_bg',$single['snap_res'],$path);
        }
    }



}